/*
 *  Netzpult - Your desk on the web
 *  Copyright (C) 2011  Ulrich Hilger, http://uhilger.de
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/
 */

package de.uhilger.netzpult.shared;

import java.io.Serializable;
import java.util.Date;

import com.google.gwt.i18n.client.DateTimeFormat;

import de.uhilger.baselink.DBColumn;
import de.uhilger.baselink.DBPrimaryKey;
import de.uhilger.baselink.DBTable;

/**
 * Document 
 * 
 * @author Copyright (c) Ulrich Hilger, http://uhilger.de
 * @author Published under the terms and conditions of
 * the <a href="http://www.gnu.org/licenses/" target="_blank">GNU General Public License</a>
 */
@DBTable(name = "webfiles.documents")
@DBPrimaryKey({"dc_id","dc_version"})
public class Document implements Serializable, Comparable<Document> {

	private static final long serialVersionUID = -5203475717608462778L;
	
	public static final int NEW_DOCUMENT_ID = -1;
	public static final int NEW_VERSION_ID = -1;
	public static final int UNORDERED = -1;
	public static final int DRAFT_VERSION_ID = -2;
	public static final int NO_VERSION_ID = -3;
	
	public static final String NEW_DOCUMENT_TITLE = "[neuer Eintrag]";
	
	/** 
	 * vgl. http://de.selfhtml.org/diverses/mimetypen.htm
	 */
	public static final String DEFAULT_MIME_TYPE = "text/html";

	private int id = NEW_DOCUMENT_ID;
	private String title = NEW_DOCUMENT_TITLE;
	private String content;
	private String creationTime; // YYYYMMDDHHMMSSmmmm GMT
	private int version = NEW_VERSION_ID;
	private String mimeType = DEFAULT_MIME_TYPE;
	//private String creationTimeStr;
	private Date creationDateTime;
	
	private int order = UNORDERED;

	public Document() {
		super();
	}

	public String getCreationTimeStr() {
		return DateTimeFormat.getFormat("dd. MMM. yyyy, HH:mm:ss Z z").format(getCreationDateTime());
	}

	/*public void setCreationTimeStr(String creationTimeStr) {
		this.creationTimeStr = creationTimeStr;
	}*/

	public int getOrder() {
		return order;
	}

	public void setOrder(int order) {
		this.order = order;
	}

	@DBColumn(name = "dc_id")
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Date getCreationDateTime() {
		return creationDateTime;
	}

	public void setCreationDateTime(Date creationDateTime) {
		this.creationDateTime = creationDateTime;
	}

	@DBColumn(name = "dc_title")
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@DBColumn(name="dc_content", type=DBColumn.Type.BLOB)
	public String getContent() {
		return content;
	}

	public void setContent(String html) {
		this.content = html;
	}

	@DBColumn(name = "dc_timestamp")
	public String getCreationTime() {
		return creationTime;
	}

	public void setCreationTime(String creationTime) {
		this.creationTime = creationTime;
		setCreationDateTime(getDocumentDate(creationTime));
	}
		
	public Date getDocumentDate(String creationTime) {
		/*
		GregorianCalendar cal = new GregorianCalendar();
		cal.set(Calendar.YEAR, Integer.parseInt(creationTime.substring(0, 4)));
		cal.set(Calendar.MONTH, Integer.parseInt(creationTime.substring(4, 6)) - 1);
		cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(creationTime.substring(6, 8)));
		cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(creationTime.substring(8, 10)));
		cal.set(Calendar.MINUTE, Integer.parseInt(creationTime.substring(10, 12)));
		cal.set(Calendar.SECOND, Integer.parseInt(creationTime.substring(12, 14)));
				
		return cal.getTime();
		*/
		
		
		String yearStr = creationTime.substring(0, 4);
		String monthStr = creationTime.substring(4, 6);
		String dayStr = creationTime.substring(6, 8);
		String hourStr = creationTime.substring(8, 10);
		String minuteStr = creationTime.substring(10, 12);
		String secondStr = creationTime.substring(12, 14);
		String millisecondStr = creationTime.substring(14, 17);
		//setCreationTimeStr(
			//	dayStr + "." + monthStr + "." + yearStr + " " + hourStr + ":" + minuteStr + ":" + secondStr);
		
		Date docDate = new Date();
		docDate.setYear(Integer.parseInt(yearStr)-1900);
		//System.out.println("Year=" + yearStr);
		docDate.setMonth(Integer.parseInt(monthStr) - 1);
		//System.out.println("Month=" + (Integer.parseInt(monthStr) - 1));
		docDate.setDate(Integer.parseInt(dayStr));
		//System.out.println("Day=" + (Integer.parseInt(dayStr)));
		docDate.setHours(Integer.parseInt(hourStr));
		//System.out.println("Hour=" + (Integer.parseInt(hourStr)));
		docDate.setMinutes(Integer.parseInt(minuteStr));
		//System.out.println("Minute=" + (Integer.parseInt(minuteStr)));
		docDate.setSeconds(Integer.parseInt(secondStr));
		//System.out.println("Second=" + (Integer.parseInt(secondStr)));
		return docDate;
	}

	

	@DBColumn(name = "dc_version")
	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Document) {
			return (getId() == ((Document) obj).getId());
		}
		return false;
	}

	@Override
	public int compareTo(Document o) {
		return this.getTitle().compareTo(o.getTitle());
	}
	
	@DBColumn(name = "dc_mimetype")
	public String getMimeType() {
		return mimeType;
	}

	public void setMimeType(String mimeType) {
		this.mimeType = mimeType;
	}
	
}
